package com.cloudsigns.factorymanager2.engineering.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.IView.IEngineeringOrderListForSuperView;
import com.cloudsigns.factorymanager2.engineering.bean.OrderParentBean;
import com.cloudsigns.factorymanager2.engineering.presenter.EngineeringOrderListForSuperPresenter;
import com.cloudsigns.factorymanager2.project.activity.AddBatchInstallationOrderActivity;
import com.cloudsigns.factorymanager2.project.activity.AddEngineeringConstructOrderActivity;
import com.cloudsigns.factorymanager2.project.activity.AddEngineeringInstallationOrderActivity;
import com.cloudsigns.factorymanager2.project.activity.AddEngineeringInstallationOrderActivity1;
import com.cloudsigns.factorymanager2.project.activity.AddEngineeringSurveyOrderActivity;
import com.cloudsigns.factorymanager2.project.activity.AddMaintenanceOrderActivity;
import com.cloudsigns.factorymanager2.project.adapter.EngineeringOrderListAdapter;
import com.cloudsigns.factorymanager2.project.customview.DropDownPoupWindow;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 新建工单
 * Created by tony on 2018/3/17.
 */

public class CreateEngineeringOrderListActivity extends Activity implements View.OnClickListener, IEngineeringOrderListForSuperView {
    private RelativeLayout rl_right;
    //private ImageView img_right;
    private ImageView img_tri_right;
    private TextView tv_title;
    private ListView lv;
    private ViewGroup rl_back;
    private DropDownPoupWindow popupWindow;
    private EngineeringOrderListAdapter adapter;
    private List<OrderParentBean> designTypeBeanList = new ArrayList<>();
    private EngineeringOrderListForSuperPresenter engineeringOrderListPresenter;
    private String TAG = "CreateEngineeringOrderListActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_create_engineering_order_list);

        engineeringOrderListPresenter = new EngineeringOrderListForSuperPresenter(this);
        init();
        getOrderList();
    }

    private void init() {

        rl_right = findViewById(R.id.rl_right);
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
       // img_right = findViewById(R.id.img_right);
        img_tri_right=findViewById(R.id.img_tri_right);
        lv = findViewById(R.id.lv);

//        tv_title.setText(getResources().getText(R.string.create_order));
        tv_title.setText("工程工单");
       // img_right.setBackgroundResource(R.mipmap.title_icon_menu);
       // rl_right.setVisibility(View.VISIBLE);
        rl_back.setOnClickListener(this);
        rl_right.setOnClickListener(this);

        adapter = new EngineeringOrderListAdapter(this, TAG);
        adapter.setData(designTypeBeanList);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                Intent mIntent;
                OrderParentBean order = designTypeBeanList.get(position);
                // 工程安装
                if (order.getCategory() == CommonEnum.EngineeringORderCategory.INSTALL) {
                    mIntent = new Intent(CreateEngineeringOrderListActivity.this, SendOrderForSuperActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, order);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                }
                // 工程施工
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.CONSTRUCT) {
                    mIntent = new Intent(CreateEngineeringOrderListActivity.this, SendOrderForSuperActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, order);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                }
                // 批量安装
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.BATCH_INSTALL) {
                    mIntent = new Intent(CreateEngineeringOrderListActivity.this, DispatchEngineeringBatchInstallationOrderActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, order);
                    startActivity(mIntent);
//                    mIntent = new Intent(CreateEngineeringOrderListActivity.this, EditBatchInstallationEngineeringActivity.class);
//                    startActivity(mIntent);
                }
                // 维修
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.MAINTAIN) {
                    mIntent = new Intent(CreateEngineeringOrderListActivity.this, DispatchEngineeringMaintenanceOrderActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, order);
                    startActivity(mIntent);
                }
                // 广告上刊
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.PUBLICATION) {
                    mIntent = new Intent(CreateEngineeringOrderListActivity.this, SendOrderForSuperActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, order);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                }
                // 巡查
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.PATROL) {
                    mIntent = new Intent(CreateEngineeringOrderListActivity.this, SendOrderForSuperActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, order);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                }
                // 现场勘查
                else if (order.getCategory() == CommonEnum.EngineeringORderCategory.SURVEY) {
                    mIntent = new Intent(CreateEngineeringOrderListActivity.this, SendOrderForSuperActivity.class);
                    mIntent.putExtra(ConstantUtil.KEY_1, order);
                    startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
                }
            }
        });
    }

    private void getOrderList() {
        engineeringOrderListPresenter.getEngineeringOrderList();
    }

    /**
     * 设置添加屏幕的背景透明�?
     *
     * @param bgAlpha
     */
    private void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = bgAlpha;
        getWindow().setAttributes(lp);
    }

    private void showPopupWindow(View anchor) {
        popupWindow = new DropDownPoupWindow(CreateEngineeringOrderListActivity.this, TAG,-1, R.layout.popwindow_work_sheets, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
       // backgroundAlpha(0.4f);
        img_tri_right.setVisibility(View.VISIBLE);
        popupWindow.setOnClickListener(this);
        popupWindow.setCusOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
                img_tri_right.setVisibility(View.GONE);
            }
        });
        popupWindow.showPopupWindow(anchor, anchor.getLayoutParams().width / 2, 5);
    }

    @Override
    public void onClick(View view) {
        Intent mIntent;

        switch (view.getId()) {
            case R.id.rl_right:
                showPopupWindow(view);
                break;
            case R.id.rl_back:
                finish();
                break;
//            case R.id.tv_design_order:  //设计联系单
//                mIntent = new Intent(CreateEngineeringOrderListActivity.this, DesignContactOrderActivity.class);
//                startActivity(mIntent);
//                if (popupWindow != null)
//                    popupWindow.dismiss();
//                break;
//            case R.id.tv_production_task_order:  //生产任务单
//                mIntent = new Intent(CreateEngineeringOrderListActivity.this, ProductionTaskOrderActivity.class);
//                startActivity(mIntent);
//                if (popupWindow != null)
//                    popupWindow.dismiss();
//                break;
//            case R.id.tv_scene_investigateorder: //现场勘察单
//                break;
            case R.id.tv_xinachangkancha://现场勘察单
                mIntent = new Intent(CreateEngineeringOrderListActivity.this, AddEngineeringSurveyOrderActivity.class);
                startActivityForResult(mIntent, ConstantUtil.CREATED_ORDER);
                if (popupWindow != null)
                    popupWindow.dismiss();
                break;
            case R.id.tv_weixiu: //维修
               // mIntent = new Intent(CreateEngineeringOrderListActivity.this, AddEngineeringInstallationOrderActivity.class);
                mIntent = new Intent(CreateEngineeringOrderListActivity.this, AddMaintenanceOrderActivity.class);
                startActivityForResult(mIntent, ConstantUtil.CREATED_ORDER);
                if (popupWindow != null)
                    popupWindow.dismiss();
                break;
            case R.id.tv_engineering_installation_order: // 工程安装单
               // mIntent = new Intent(CreateEngineeringOrderListActivity.this, AddEngineeringInstallationOrderActivity.class);
                mIntent = new Intent(CreateEngineeringOrderListActivity.this, AddEngineeringInstallationOrderActivity1.class);
                startActivityForResult(mIntent, ConstantUtil.CREATED_ORDER);
                if (popupWindow != null)
                    popupWindow.dismiss();
                break;
            case R.id.tv_engineering_construct_order:  // 工程施工单
                mIntent = new Intent(CreateEngineeringOrderListActivity.this, AddEngineeringConstructOrderActivity.class);
                startActivityForResult(mIntent, ConstantUtil.CREATED_ORDER);

                if (popupWindow != null)
                    popupWindow.dismiss();
                break;
            case R.id.tv_batch_installation_order:  // 批量安装单
                mIntent = new Intent(CreateEngineeringOrderListActivity.this, AddBatchInstallationOrderActivity.class);
                startActivityForResult(mIntent, ConstantUtil.CREATED_ORDER);

                if (popupWindow != null)
                    popupWindow.dismiss();
                break;
            case R.id.tv_publication:  // 广告上刊
               // mIntent = new Intent(CreateEngineeringOrderListActivity.this, AddPublicationOrderActivity.class);
                mIntent = new Intent(CreateEngineeringOrderListActivity.this, AddPublicationOrderActivity1.class);
                startActivityForResult(mIntent, ConstantUtil.CREATED_ORDER);

                if (popupWindow != null)
                    popupWindow.dismiss();
                break;
            case R.id.tv_patrol:  // 巡查
               // mIntent = new Intent(CreateEngineeringOrderListActivity.this, AddPartrolOrderActivity.class);
                mIntent = new Intent(CreateEngineeringOrderListActivity.this, AddPartrolOrderActivity1.class);
                startActivityForResult(mIntent, ConstantUtil.CREATED_ORDER);

                if (popupWindow != null)
                    popupWindow.dismiss();
                break;
//
//            case R.id.tv_maintenance_order:  //维修单
////                mIntent = new Intent(CreateEngineeringOrderListActivity.this, AddMaintenanceOrderActivity.class);
////                startActivityForResult(mIntent, ConstantUtil.CREATED_ORDER);
////
////                if (popupWindow != null)
////                    popupWindow.dismiss();
//                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.CREATED_ORDER: //创建工单
                if (data != null) {
                    Boolean update = (Boolean) data.getBooleanExtra(ConstantUtil.KEY_1, false);
                    if (update) {
                        getOrderList();
                    }


                }
                break;
            case ConstantUtil.START_ACTIVITY_KEY_1:
                if (null != data)
                    getOrderList();
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (popupWindow != null) {
            popupWindow.dismiss();
            popupWindow = null;
        }
    }

    @Override
    public void getDataSuccessed(List<OrderParentBean> engineeringOrderBeanLs) {
        if (null != engineeringOrderBeanLs) {
            designTypeBeanList.clear();
            designTypeBeanList.addAll(engineeringOrderBeanLs);
            adapter.setData(designTypeBeanList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showFailedError(String desc) {
        Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }
}
